
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta charset="utf-8" />
  
  <!-- Licensed under the Apache 2.0 License -->
  <link rel="stylesheet" type="text/css" href="../../../_static/fonts/open-sans/stylesheet.css" />
  <!-- Licensed under the SIL Open Font License -->
  <link rel="stylesheet" type="text/css" href="../../../_static/fonts/source-serif-pro/source-serif-pro.css" />
  <link rel="stylesheet" type="text/css" href="../../../_static/css/bootstrap.min.css" />
  <link rel="stylesheet" type="text/css" href="../../../_static/css/bootstrap-theme.min.css" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
    <title>jmetal.operator.selection &#8212; jMetalPy 1.5.1 documentation</title>
    <link rel="stylesheet" href="../../../_static/guzzle.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <link rel="stylesheet" type="text/css" href="../../../_static/custom.css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../_static/language_data.js"></script>
    <script async="async" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <link rel="author" title="About these documents" href="../../../about.html" />
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />
  
   

  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">jMetalPy 1.5.1 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" accesskey="U">Module code</a> &#187;</li> 
      </ul>
    </div>
    <div class="container-wrapper">

      <div id="mobile-toggle">
        <a href="#"><span class="glyphicon glyphicon-align-justify" aria-hidden="true"></span></a>
      </div>
  <div id="left-column">
    <div class="sphinxsidebar"><a href="
    ../../../index.html" class="text-logo">jMetalPy</a>
<div class="sidebar-block">
  <div class="sidebar-wrapper">
    <h2>Table Of Contents</h2>
  </div>
  <div class="sidebar-toc">
    
    
      <ul>
<li class="toctree-l1"><a class="reference internal" href="../../../tutorials.html">Getting started</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../multiobjective.algorithms.html">Multi-objective algorithms</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../singleobjective.algorithms.html">Single-objective algorithms</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../operators.html">Operators</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../problems.html">Problems</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../contributing.html">Contributing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../about.html">About</a></li>
</ul>

    
  </div>
</div>
<div class="sidebar-block">
  <div class="sidebar-wrapper">
    <div id="main-search">
      <form class="form-inline" action="../../../search.html" method="GET" role="form">
        <div class="input-group">
          <input name="q" type="text" class="form-control" placeholder="Search...">
        </div>
        <input type="hidden" name="check_keywords" value="yes" />
        <input type="hidden" name="area" value="default" />
      </form>
    </div>
  </div>
</div>
      
    </div>
  </div>
        <div id="right-column">
          
          <div role="navigation" aria-label="breadcrumbs navigation">
            <ol class="breadcrumb">
              <li><a href="../../../index.html">Docs</a></li>
              
                <li><a href="../../index.html">Module code</a></li>
              
              <li>jmetal.operator.selection</li>
            </ol>
          </div>
          
          <div class="document clearer body">
            
  <h1>Source code for jmetal.operator.selection</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">random</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="k">import</span> <span class="n">List</span><span class="p">,</span> <span class="n">TypeVar</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>

<span class="kn">from</span> <span class="nn">jmetal.core.operator</span> <span class="k">import</span> <span class="n">Selection</span>
<span class="kn">from</span> <span class="nn">jmetal.util.density_estimator</span> <span class="k">import</span> <span class="n">CrowdingDistance</span>
<span class="kn">from</span> <span class="nn">jmetal.util.ranking</span> <span class="k">import</span> <span class="n">FastNonDominatedRanking</span>
<span class="kn">from</span> <span class="nn">jmetal.util.solutions.comparator</span> <span class="k">import</span> <span class="n">Comparator</span><span class="p">,</span> <span class="n">DominanceComparator</span>

<span class="n">S</span> <span class="o">=</span> <span class="n">TypeVar</span><span class="p">(</span><span class="s1">&#39;S&#39;</span><span class="p">)</span>

<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">.. module:: selection</span>
<span class="sd">   :platform: Unix, Windows</span>
<span class="sd">   :synopsis: Module implementing selection operators.</span>

<span class="sd">.. moduleauthor:: Antonio J. Nebro &lt;antonio@lcc.uma.es&gt;, Antonio Benítez-Hidalgo &lt;antonio.b@uma.es&gt;</span>
<span class="sd">&quot;&quot;&quot;</span>


<div class="viewcode-block" id="RouletteWheelSelection"><a class="viewcode-back" href="../../../api/operator/selection.html#jmetal.operator.selection.RouletteWheelSelection">[docs]</a><span class="k">class</span> <span class="nc">RouletteWheelSelection</span><span class="p">(</span><span class="n">Selection</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="n">S</span><span class="p">],</span> <span class="n">S</span><span class="p">]):</span>
    <span class="sd">&quot;&quot;&quot;Performs roulette wheel selection.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">RouletteWheelSelection</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>

<div class="viewcode-block" id="RouletteWheelSelection.execute"><a class="viewcode-back" href="../../../api/operator/selection.html#jmetal.operator.selection.RouletteWheelSelection.execute">[docs]</a>    <span class="k">def</span> <span class="nf">execute</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">front</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">S</span><span class="p">])</span> <span class="o">-&gt;</span> <span class="n">S</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">front</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">&#39;The front is null&#39;</span><span class="p">)</span>
        <span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">front</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">&#39;The front is empty&#39;</span><span class="p">)</span>

        <span class="n">maximum</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">([</span><span class="n">solution</span><span class="o">.</span><span class="n">objectives</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">solution</span> <span class="ow">in</span> <span class="n">front</span><span class="p">])</span>
        <span class="n">rand</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">uniform</span><span class="p">(</span><span class="mf">0.0</span><span class="p">,</span> <span class="n">maximum</span><span class="p">)</span>
        <span class="n">value</span> <span class="o">=</span> <span class="mf">0.0</span>

        <span class="k">for</span> <span class="n">solution</span> <span class="ow">in</span> <span class="n">front</span><span class="p">:</span>
            <span class="n">value</span> <span class="o">+=</span> <span class="n">solution</span><span class="o">.</span><span class="n">objectives</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>

            <span class="k">if</span> <span class="n">value</span> <span class="o">&gt;</span> <span class="n">rand</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">solution</span>

        <span class="k">return</span> <span class="kc">None</span></div>

<div class="viewcode-block" id="RouletteWheelSelection.get_name"><a class="viewcode-back" href="../../../api/operator/selection.html#jmetal.operator.selection.RouletteWheelSelection.get_name">[docs]</a>    <span class="k">def</span> <span class="nf">get_name</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">str</span><span class="p">:</span>
        <span class="k">return</span> <span class="s1">&#39;Roulette wheel selection&#39;</span></div></div>


<div class="viewcode-block" id="BinaryTournamentSelection"><a class="viewcode-back" href="../../../api/operator/selection.html#jmetal.operator.selection.BinaryTournamentSelection">[docs]</a><span class="k">class</span> <span class="nc">BinaryTournamentSelection</span><span class="p">(</span><span class="n">Selection</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="n">S</span><span class="p">],</span> <span class="n">S</span><span class="p">]):</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">comparator</span><span class="p">:</span> <span class="n">Comparator</span> <span class="o">=</span> <span class="n">DominanceComparator</span><span class="p">()):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">BinaryTournamentSelection</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">comparator</span> <span class="o">=</span> <span class="n">comparator</span>

<div class="viewcode-block" id="BinaryTournamentSelection.execute"><a class="viewcode-back" href="../../../api/operator/selection.html#jmetal.operator.selection.BinaryTournamentSelection.execute">[docs]</a>    <span class="k">def</span> <span class="nf">execute</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">front</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">S</span><span class="p">])</span> <span class="o">-&gt;</span> <span class="n">S</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">front</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">&#39;The front is null&#39;</span><span class="p">)</span>
        <span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">front</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">&#39;The front is empty&#39;</span><span class="p">)</span>

        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">front</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">result</span> <span class="o">=</span> <span class="n">front</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># Sampling without replacement</span>
            <span class="n">i</span><span class="p">,</span> <span class="n">j</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">sample</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">front</span><span class="p">)),</span> <span class="mi">2</span><span class="p">)</span>
            <span class="n">solution1</span> <span class="o">=</span> <span class="n">front</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
            <span class="n">solution2</span> <span class="o">=</span> <span class="n">front</span><span class="p">[</span><span class="n">j</span><span class="p">]</span>

            <span class="n">flag</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">comparator</span><span class="o">.</span><span class="n">compare</span><span class="p">(</span><span class="n">solution1</span><span class="p">,</span> <span class="n">solution2</span><span class="p">)</span>

            <span class="k">if</span> <span class="n">flag</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="p">:</span>
                <span class="n">result</span> <span class="o">=</span> <span class="n">solution1</span>
            <span class="k">elif</span> <span class="n">flag</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
                <span class="n">result</span> <span class="o">=</span> <span class="n">solution2</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">result</span> <span class="o">=</span> <span class="p">[</span><span class="n">solution1</span><span class="p">,</span> <span class="n">solution2</span><span class="p">][</span><span class="n">random</span><span class="o">.</span><span class="n">random</span><span class="p">()</span> <span class="o">&lt;</span> <span class="mf">0.5</span><span class="p">]</span>

        <span class="k">return</span> <span class="n">result</span></div>

<div class="viewcode-block" id="BinaryTournamentSelection.get_name"><a class="viewcode-back" href="../../../api/operator/selection.html#jmetal.operator.selection.BinaryTournamentSelection.get_name">[docs]</a>    <span class="k">def</span> <span class="nf">get_name</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">str</span><span class="p">:</span>
        <span class="k">return</span> <span class="s1">&#39;Binary tournament selection&#39;</span></div></div>


<div class="viewcode-block" id="BestSolutionSelection"><a class="viewcode-back" href="../../../api/operator/selection.html#jmetal.operator.selection.BestSolutionSelection">[docs]</a><span class="k">class</span> <span class="nc">BestSolutionSelection</span><span class="p">(</span><span class="n">Selection</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="n">S</span><span class="p">],</span> <span class="n">S</span><span class="p">]):</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">BestSolutionSelection</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>

<div class="viewcode-block" id="BestSolutionSelection.execute"><a class="viewcode-back" href="../../../api/operator/selection.html#jmetal.operator.selection.BestSolutionSelection.execute">[docs]</a>    <span class="k">def</span> <span class="nf">execute</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">front</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">S</span><span class="p">])</span> <span class="o">-&gt;</span> <span class="n">S</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">front</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">&#39;The front is null&#39;</span><span class="p">)</span>
        <span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">front</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">&#39;The front is empty&#39;</span><span class="p">)</span>

        <span class="n">result</span> <span class="o">=</span> <span class="n">front</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>

        <span class="k">for</span> <span class="n">solution</span> <span class="ow">in</span> <span class="n">front</span><span class="p">[</span><span class="mi">1</span><span class="p">:]:</span>
            <span class="k">if</span> <span class="n">DominanceComparator</span><span class="p">()</span><span class="o">.</span><span class="n">compare</span><span class="p">(</span><span class="n">solution</span><span class="p">,</span> <span class="n">result</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">result</span> <span class="o">=</span> <span class="n">solution</span>

        <span class="k">return</span> <span class="n">result</span></div>

<div class="viewcode-block" id="BestSolutionSelection.get_name"><a class="viewcode-back" href="../../../api/operator/selection.html#jmetal.operator.selection.BestSolutionSelection.get_name">[docs]</a>    <span class="k">def</span> <span class="nf">get_name</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">str</span><span class="p">:</span>
        <span class="k">return</span> <span class="s1">&#39;Best solution selection&#39;</span></div></div>


<div class="viewcode-block" id="NaryRandomSolutionSelection"><a class="viewcode-back" href="../../../api/operator/selection.html#jmetal.operator.selection.NaryRandomSolutionSelection">[docs]</a><span class="k">class</span> <span class="nc">NaryRandomSolutionSelection</span><span class="p">(</span><span class="n">Selection</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="n">S</span><span class="p">],</span> <span class="n">S</span><span class="p">]):</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">number_of_solutions_to_be_returned</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">1</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">NaryRandomSolutionSelection</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">number_of_solutions_to_be_returned</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">&#39;The number of solutions to be returned must be positive integer&#39;</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">number_of_solutions_to_be_returned</span> <span class="o">=</span> <span class="n">number_of_solutions_to_be_returned</span>

<div class="viewcode-block" id="NaryRandomSolutionSelection.execute"><a class="viewcode-back" href="../../../api/operator/selection.html#jmetal.operator.selection.NaryRandomSolutionSelection.execute">[docs]</a>    <span class="k">def</span> <span class="nf">execute</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">front</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">S</span><span class="p">])</span> <span class="o">-&gt;</span> <span class="n">S</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">front</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">&#39;The front is null&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">front</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">&#39;The front is empty&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">front</span><span class="p">)</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">number_of_solutions_to_be_returned</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">&#39;The front contains less elements than required&#39;</span><span class="p">)</span>

        <span class="c1"># random sampling without replacement</span>
        <span class="k">return</span> <span class="n">random</span><span class="o">.</span><span class="n">sample</span><span class="p">(</span><span class="n">front</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">number_of_solutions_to_be_returned</span><span class="p">)</span></div>

<div class="viewcode-block" id="NaryRandomSolutionSelection.get_name"><a class="viewcode-back" href="../../../api/operator/selection.html#jmetal.operator.selection.NaryRandomSolutionSelection.get_name">[docs]</a>    <span class="k">def</span> <span class="nf">get_name</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">str</span><span class="p">:</span>
        <span class="k">return</span> <span class="s1">&#39;Nary random solution selection&#39;</span></div></div>


<div class="viewcode-block" id="DifferentialEvolutionSelection"><a class="viewcode-back" href="../../../api/operator/selection.html#jmetal.operator.selection.DifferentialEvolutionSelection">[docs]</a><span class="k">class</span> <span class="nc">DifferentialEvolutionSelection</span><span class="p">(</span><span class="n">Selection</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="n">S</span><span class="p">],</span> <span class="n">List</span><span class="p">[</span><span class="n">S</span><span class="p">]]):</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">DifferentialEvolutionSelection</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">index_to_exclude</span> <span class="o">=</span> <span class="kc">None</span>

<div class="viewcode-block" id="DifferentialEvolutionSelection.execute"><a class="viewcode-back" href="../../../api/operator/selection.html#jmetal.operator.selection.DifferentialEvolutionSelection.execute">[docs]</a>    <span class="k">def</span> <span class="nf">execute</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">front</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">S</span><span class="p">])</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">S</span><span class="p">]:</span>
        <span class="k">if</span> <span class="n">front</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">&#39;The front is null&#39;</span><span class="p">)</span>
        <span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">front</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">&#39;The front is empty&#39;</span><span class="p">)</span>
        <span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">front</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mi">4</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">&#39;The front has less than four solutions: &#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">front</span><span class="p">)))</span>

        <span class="n">selected_indexes</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">sample</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">front</span><span class="p">)),</span> <span class="mi">3</span><span class="p">)</span>
        <span class="k">while</span> <span class="bp">self</span><span class="o">.</span><span class="n">index_to_exclude</span> <span class="ow">in</span> <span class="n">selected_indexes</span><span class="p">:</span>
            <span class="n">selected_indexes</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">sample</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">front</span><span class="p">)),</span> <span class="mi">3</span><span class="p">)</span>

        <span class="k">return</span> <span class="p">[</span><span class="n">front</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">selected_indexes</span><span class="p">]</span></div>

<div class="viewcode-block" id="DifferentialEvolutionSelection.set_index_to_exclude"><a class="viewcode-back" href="../../../api/operator/selection.html#jmetal.operator.selection.DifferentialEvolutionSelection.set_index_to_exclude">[docs]</a>    <span class="k">def</span> <span class="nf">set_index_to_exclude</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">index</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">index_to_exclude</span> <span class="o">=</span> <span class="n">index</span></div>

<div class="viewcode-block" id="DifferentialEvolutionSelection.get_name"><a class="viewcode-back" href="../../../api/operator/selection.html#jmetal.operator.selection.DifferentialEvolutionSelection.get_name">[docs]</a>    <span class="k">def</span> <span class="nf">get_name</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">str</span><span class="p">:</span>
        <span class="k">return</span> <span class="s2">&quot;Differential evolution selection&quot;</span></div></div>


<div class="viewcode-block" id="RandomSolutionSelection"><a class="viewcode-back" href="../../../api/operator/selection.html#jmetal.operator.selection.RandomSolutionSelection">[docs]</a><span class="k">class</span> <span class="nc">RandomSolutionSelection</span><span class="p">(</span><span class="n">Selection</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="n">S</span><span class="p">],</span> <span class="n">S</span><span class="p">]):</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">RandomSolutionSelection</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>

<div class="viewcode-block" id="RandomSolutionSelection.execute"><a class="viewcode-back" href="../../../api/operator/selection.html#jmetal.operator.selection.RandomSolutionSelection.execute">[docs]</a>    <span class="k">def</span> <span class="nf">execute</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">front</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">S</span><span class="p">])</span> <span class="o">-&gt;</span> <span class="n">S</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">front</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">&#39;The front is null&#39;</span><span class="p">)</span>
        <span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">front</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">&#39;The front is empty&#39;</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">random</span><span class="o">.</span><span class="n">choice</span><span class="p">(</span><span class="n">front</span><span class="p">)</span></div>

<div class="viewcode-block" id="RandomSolutionSelection.get_name"><a class="viewcode-back" href="../../../api/operator/selection.html#jmetal.operator.selection.RandomSolutionSelection.get_name">[docs]</a>    <span class="k">def</span> <span class="nf">get_name</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">str</span><span class="p">:</span>
        <span class="k">return</span> <span class="s1">&#39;Random solution selection&#39;</span></div></div>


<div class="viewcode-block" id="RankingAndCrowdingDistanceSelection"><a class="viewcode-back" href="../../../api/operator/selection.html#jmetal.operator.selection.RankingAndCrowdingDistanceSelection">[docs]</a><span class="k">class</span> <span class="nc">RankingAndCrowdingDistanceSelection</span><span class="p">(</span><span class="n">Selection</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="n">S</span><span class="p">],</span> <span class="n">List</span><span class="p">[</span><span class="n">S</span><span class="p">]]):</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">max_population_size</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">dominance_comparator</span><span class="p">:</span> <span class="n">Comparator</span> <span class="o">=</span> <span class="n">DominanceComparator</span><span class="p">()):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">RankingAndCrowdingDistanceSelection</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">max_population_size</span> <span class="o">=</span> <span class="n">max_population_size</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">dominance_comparator</span> <span class="o">=</span> <span class="n">dominance_comparator</span>

<div class="viewcode-block" id="RankingAndCrowdingDistanceSelection.execute"><a class="viewcode-back" href="../../../api/operator/selection.html#jmetal.operator.selection.RankingAndCrowdingDistanceSelection.execute">[docs]</a>    <span class="k">def</span> <span class="nf">execute</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">front</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">S</span><span class="p">])</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">S</span><span class="p">]:</span>
        <span class="k">if</span> <span class="n">front</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">&#39;The front is null&#39;</span><span class="p">)</span>
        <span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">front</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">&#39;The front is empty&#39;</span><span class="p">)</span>

        <span class="n">ranking</span> <span class="o">=</span> <span class="n">FastNonDominatedRanking</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">dominance_comparator</span><span class="p">)</span>
        <span class="n">crowding_distance</span> <span class="o">=</span> <span class="n">CrowdingDistance</span><span class="p">()</span>
        <span class="n">ranking</span><span class="o">.</span><span class="n">compute_ranking</span><span class="p">(</span><span class="n">front</span><span class="p">)</span>

        <span class="n">ranking_index</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="n">new_solution_list</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="k">while</span> <span class="nb">len</span><span class="p">(</span><span class="n">new_solution_list</span><span class="p">)</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_population_size</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">ranking</span><span class="o">.</span><span class="n">get_subfront</span><span class="p">(</span><span class="n">ranking_index</span><span class="p">))</span> <span class="o">&lt;</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">max_population_size</span> <span class="o">-</span> <span class="nb">len</span><span class="p">(</span><span class="n">new_solution_list</span><span class="p">)):</span>
                <span class="n">new_solution_list</span> <span class="o">=</span> <span class="n">new_solution_list</span> <span class="o">+</span> <span class="n">ranking</span><span class="o">.</span><span class="n">get_subfront</span><span class="p">(</span><span class="n">ranking_index</span><span class="p">)</span>
                <span class="n">ranking_index</span> <span class="o">+=</span> <span class="mi">1</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">subfront</span> <span class="o">=</span> <span class="n">ranking</span><span class="o">.</span><span class="n">get_subfront</span><span class="p">(</span><span class="n">ranking_index</span><span class="p">)</span>
                <span class="n">crowding_distance</span><span class="o">.</span><span class="n">compute_density_estimator</span><span class="p">(</span><span class="n">subfront</span><span class="p">)</span>
                <span class="n">sorted_subfront</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">subfront</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="o">.</span><span class="n">attributes</span><span class="p">[</span><span class="s1">&#39;crowding_distance&#39;</span><span class="p">],</span> <span class="n">reverse</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
                <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">max_population_size</span> <span class="o">-</span> <span class="nb">len</span><span class="p">(</span><span class="n">new_solution_list</span><span class="p">))):</span>
                    <span class="n">new_solution_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">sorted_subfront</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>

        <span class="k">return</span> <span class="n">new_solution_list</span></div>

<div class="viewcode-block" id="RankingAndCrowdingDistanceSelection.get_name"><a class="viewcode-back" href="../../../api/operator/selection.html#jmetal.operator.selection.RankingAndCrowdingDistanceSelection.get_name">[docs]</a>    <span class="k">def</span> <span class="nf">get_name</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">str</span><span class="p">:</span>
        <span class="k">return</span> <span class="s1">&#39;Ranking and crowding distance selection&#39;</span></div></div>


<div class="viewcode-block" id="RankingAndFitnessSelection"><a class="viewcode-back" href="../../../api/operator/selection.html#jmetal.operator.selection.RankingAndFitnessSelection">[docs]</a><span class="k">class</span> <span class="nc">RankingAndFitnessSelection</span><span class="p">(</span><span class="n">Selection</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="n">S</span><span class="p">],</span> <span class="n">List</span><span class="p">[</span><span class="n">S</span><span class="p">]]):</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>
                 <span class="n">max_population_size</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">reference_point</span><span class="p">:</span> <span class="n">S</span><span class="p">,</span>
                 <span class="n">dominance_comparator</span><span class="p">:</span> <span class="n">Comparator</span> <span class="o">=</span> <span class="n">DominanceComparator</span><span class="p">()):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">RankingAndFitnessSelection</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">max_population_size</span> <span class="o">=</span> <span class="n">max_population_size</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">dominance_comparator</span> <span class="o">=</span> <span class="n">dominance_comparator</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">reference_point</span> <span class="o">=</span> <span class="n">reference_point</span>

<div class="viewcode-block" id="RankingAndFitnessSelection.hypesub"><a class="viewcode-back" href="../../../api/operator/selection.html#jmetal.operator.selection.RankingAndFitnessSelection.hypesub">[docs]</a>    <span class="k">def</span> <span class="nf">hypesub</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">l</span><span class="p">,</span> <span class="n">A</span><span class="p">,</span> <span class="n">actDim</span><span class="p">,</span> <span class="n">bounds</span><span class="p">,</span> <span class="n">pvec</span><span class="p">,</span> <span class="n">alpha</span><span class="p">,</span> <span class="n">k</span><span class="p">):</span>
        <span class="n">h</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">l</span><span class="p">)]</span>
        <span class="n">Adim</span> <span class="o">=</span> <span class="p">[</span><span class="n">a</span><span class="p">[</span><span class="n">actDim</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">A</span><span class="p">]</span>
        <span class="n">indices_sort</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">Adim</span><span class="p">)),</span> <span class="n">key</span><span class="o">=</span><span class="n">Adim</span><span class="o">.</span><span class="fm">__getitem__</span><span class="p">)</span>
        <span class="n">S</span> <span class="o">=</span> <span class="p">[</span><span class="n">A</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="n">indices_sort</span><span class="p">]</span>
        <span class="n">pvec</span> <span class="o">=</span> <span class="p">[</span><span class="n">pvec</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="n">indices_sort</span><span class="p">]</span>

        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">S</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">S</span><span class="p">):</span>
                <span class="n">extrusion</span> <span class="o">=</span> <span class="n">S</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">actDim</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">S</span><span class="p">[</span><span class="n">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">][</span><span class="n">actDim</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">extrusion</span> <span class="o">=</span> <span class="n">bounds</span><span class="p">[</span><span class="n">actDim</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">S</span><span class="p">[</span><span class="n">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">][</span><span class="n">actDim</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span>

            <span class="k">if</span> <span class="n">actDim</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">i</span> <span class="o">&gt;</span> <span class="n">k</span><span class="p">:</span>
                    <span class="k">break</span>
                <span class="k">if</span> <span class="nb">all</span><span class="p">(</span><span class="n">alpha</span><span class="p">)</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">pvec</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="n">i</span><span class="p">]:</span>
                        <span class="n">h</span><span class="p">[</span><span class="n">p</span><span class="p">]</span> <span class="o">=</span> <span class="n">h</span><span class="p">[</span><span class="n">p</span><span class="p">]</span> <span class="o">+</span> <span class="n">extrusion</span> <span class="o">*</span> <span class="n">alpha</span><span class="p">[</span><span class="n">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">extrusion</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="n">h</span> <span class="o">=</span> <span class="p">[</span><span class="n">h</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">+</span> <span class="n">extrusion</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">hypesub</span><span class="p">(</span><span class="n">l</span><span class="p">,</span> <span class="n">S</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="n">i</span><span class="p">],</span> <span class="n">actDim</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="n">bounds</span><span class="p">,</span> <span class="n">pvec</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="n">i</span><span class="p">],</span> <span class="n">alpha</span><span class="p">,</span> <span class="n">k</span><span class="p">)[</span><span class="n">j</span><span class="p">]</span> <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span>
                         <span class="nb">range</span><span class="p">(</span><span class="n">l</span><span class="p">)]</span>

        <span class="k">return</span> <span class="n">h</span></div>

<div class="viewcode-block" id="RankingAndFitnessSelection.compute_hypervol_fitness_values"><a class="viewcode-back" href="../../../api/operator/selection.html#jmetal.operator.selection.RankingAndFitnessSelection.compute_hypervol_fitness_values">[docs]</a>    <span class="k">def</span> <span class="nf">compute_hypervol_fitness_values</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">population</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">S</span><span class="p">],</span> <span class="n">reference_point</span><span class="p">:</span> <span class="n">S</span><span class="p">,</span> <span class="n">k</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span>
        <span class="n">points</span> <span class="o">=</span> <span class="p">[</span><span class="n">ind</span><span class="o">.</span><span class="n">objectives</span> <span class="k">for</span> <span class="n">ind</span> <span class="ow">in</span> <span class="n">population</span><span class="p">]</span>
        <span class="n">bounds</span> <span class="o">=</span> <span class="n">reference_point</span><span class="o">.</span><span class="n">objectives</span>
        <span class="n">population_size</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">points</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">k</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">k</span> <span class="o">=</span> <span class="n">population_size</span>

        <span class="n">actDim</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">bounds</span><span class="p">)</span>
        <span class="n">pvec</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="n">population_size</span><span class="p">)</span>
        <span class="n">alpha</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">k</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>
            <span class="n">alpha</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">prod</span><span class="p">([</span><span class="nb">float</span><span class="p">(</span><span class="n">k</span> <span class="o">-</span> <span class="n">j</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">population_size</span> <span class="o">-</span> <span class="n">j</span><span class="p">)</span> <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">i</span><span class="p">)])</span> <span class="o">/</span> <span class="n">i</span><span class="p">)</span>

        <span class="n">f</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">hypesub</span><span class="p">(</span><span class="n">population_size</span><span class="p">,</span> <span class="n">points</span><span class="p">,</span> <span class="n">actDim</span><span class="p">,</span> <span class="n">bounds</span><span class="p">,</span> <span class="n">pvec</span><span class="p">,</span> <span class="n">alpha</span><span class="p">,</span> <span class="n">k</span><span class="p">)</span>

        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">population</span><span class="p">)):</span>
            <span class="n">population</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">attributes</span><span class="p">[</span><span class="s1">&#39;fitness&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">f</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>

        <span class="k">return</span> <span class="n">population</span></div>

<div class="viewcode-block" id="RankingAndFitnessSelection.execute"><a class="viewcode-back" href="../../../api/operator/selection.html#jmetal.operator.selection.RankingAndFitnessSelection.execute">[docs]</a>    <span class="k">def</span> <span class="nf">execute</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">front</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">S</span><span class="p">])</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">S</span><span class="p">]:</span>
        <span class="k">if</span> <span class="n">front</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">&#39;The front is null&#39;</span><span class="p">)</span>
        <span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">front</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">&#39;The front is empty&#39;</span><span class="p">)</span>

        <span class="n">ranking</span> <span class="o">=</span> <span class="n">FastNonDominatedRanking</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">dominance_comparator</span><span class="p">)</span>
        <span class="n">ranking</span><span class="o">.</span><span class="n">compute_ranking</span><span class="p">(</span><span class="n">front</span><span class="p">)</span>

        <span class="n">ranking_index</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="n">new_solution_list</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="k">while</span> <span class="nb">len</span><span class="p">(</span><span class="n">new_solution_list</span><span class="p">)</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_population_size</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">ranking</span><span class="o">.</span><span class="n">get_subfront</span><span class="p">(</span><span class="n">ranking_index</span><span class="p">))</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_population_size</span> <span class="o">-</span> <span class="nb">len</span><span class="p">(</span><span class="n">new_solution_list</span><span class="p">):</span>
                <span class="n">subfront</span> <span class="o">=</span> <span class="n">ranking</span><span class="o">.</span><span class="n">get_subfront</span><span class="p">(</span><span class="n">ranking_index</span><span class="p">)</span>
                <span class="n">new_solution_list</span> <span class="o">=</span> <span class="n">new_solution_list</span> <span class="o">+</span> <span class="n">subfront</span>
                <span class="n">ranking_index</span> <span class="o">+=</span> <span class="mi">1</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">subfront</span> <span class="o">=</span> <span class="n">ranking</span><span class="o">.</span><span class="n">get_subfront</span><span class="p">(</span><span class="n">ranking_index</span><span class="p">)</span>
                <span class="n">parameter_K</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">subfront</span><span class="p">)</span> <span class="o">-</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">max_population_size</span> <span class="o">-</span> <span class="nb">len</span><span class="p">(</span><span class="n">new_solution_list</span><span class="p">))</span>
                <span class="k">while</span> <span class="n">parameter_K</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="n">subfront</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">compute_hypervol_fitness_values</span><span class="p">(</span><span class="n">subfront</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">reference_point</span><span class="p">,</span> <span class="n">parameter_K</span><span class="p">)</span>
                    <span class="n">subfront</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">subfront</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="o">.</span><span class="n">attributes</span><span class="p">[</span><span class="s1">&#39;fitness&#39;</span><span class="p">],</span> <span class="n">reverse</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
                    <span class="n">subfront</span> <span class="o">=</span> <span class="n">subfront</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
                    <span class="n">parameter_K</span> <span class="o">=</span> <span class="n">parameter_K</span> <span class="o">-</span> <span class="mi">1</span>
                <span class="n">new_solution_list</span> <span class="o">=</span> <span class="n">new_solution_list</span> <span class="o">+</span> <span class="n">subfront</span>
        <span class="k">return</span> <span class="n">new_solution_list</span></div>

<div class="viewcode-block" id="RankingAndFitnessSelection.get_name"><a class="viewcode-back" href="../../../api/operator/selection.html#jmetal.operator.selection.RankingAndFitnessSelection.get_name">[docs]</a>    <span class="k">def</span> <span class="nf">get_name</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">str</span><span class="p">:</span>
        <span class="k">return</span> <span class="s1">&#39;Ranking and fitness selection&#39;</span></div></div>


<div class="viewcode-block" id="BinaryTournament2Selection"><a class="viewcode-back" href="../../../api/operator/selection.html#jmetal.operator.selection.BinaryTournament2Selection">[docs]</a><span class="k">class</span> <span class="nc">BinaryTournament2Selection</span><span class="p">(</span><span class="n">Selection</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="n">S</span><span class="p">],</span> <span class="n">S</span><span class="p">]):</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">comparator_list</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">Comparator</span><span class="p">]):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">BinaryTournament2Selection</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">comparator_list</span> <span class="o">=</span> <span class="n">comparator_list</span>

<div class="viewcode-block" id="BinaryTournament2Selection.execute"><a class="viewcode-back" href="../../../api/operator/selection.html#jmetal.operator.selection.BinaryTournament2Selection.execute">[docs]</a>    <span class="k">def</span> <span class="nf">execute</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">front</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">S</span><span class="p">])</span> <span class="o">-&gt;</span> <span class="n">S</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">front</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">&#39;The front is null&#39;</span><span class="p">)</span>
        <span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">front</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">&#39;The front is empty&#39;</span><span class="p">)</span>
        <span class="k">elif</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">comparator_list</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">&#39;The comparators</span><span class="se">\&#39;</span><span class="s1"> list is empty&#39;</span><span class="p">)</span>

        <span class="n">winner</span> <span class="o">=</span> <span class="kc">None</span>

        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">front</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">winner</span> <span class="o">=</span> <span class="n">front</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">comparator</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">comparator_list</span><span class="p">:</span>
                <span class="n">winner</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__winner</span><span class="p">(</span><span class="n">front</span><span class="p">,</span> <span class="n">comparator</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">winner</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                    <span class="k">break</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">winner</span><span class="p">:</span>
            <span class="n">i</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">randrange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">front</span><span class="p">))</span>
            <span class="n">winner</span> <span class="o">=</span> <span class="n">front</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>

        <span class="k">return</span> <span class="n">winner</span></div>

    <span class="k">def</span> <span class="nf">__winner</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">front</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">S</span><span class="p">],</span> <span class="n">comparator</span><span class="p">:</span> <span class="n">Comparator</span><span class="p">):</span>
        <span class="c1"># Sampling without replacement</span>
        <span class="n">i</span><span class="p">,</span> <span class="n">j</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">sample</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">front</span><span class="p">)),</span> <span class="mi">2</span><span class="p">)</span>

        <span class="n">solution1</span> <span class="o">=</span> <span class="n">front</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
        <span class="n">solution2</span> <span class="o">=</span> <span class="n">front</span><span class="p">[</span><span class="n">j</span><span class="p">]</span>

        <span class="n">flag</span> <span class="o">=</span> <span class="n">comparator</span><span class="o">.</span><span class="n">compare</span><span class="p">(</span><span class="n">solution1</span><span class="p">,</span> <span class="n">solution2</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">flag</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="p">:</span>
            <span class="n">result</span> <span class="o">=</span> <span class="n">solution1</span>
        <span class="k">elif</span> <span class="n">flag</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">result</span> <span class="o">=</span> <span class="n">solution2</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">result</span> <span class="o">=</span> <span class="kc">None</span>

        <span class="k">return</span> <span class="n">result</span>

<div class="viewcode-block" id="BinaryTournament2Selection.get_name"><a class="viewcode-back" href="../../../api/operator/selection.html#jmetal.operator.selection.BinaryTournament2Selection.get_name">[docs]</a>    <span class="k">def</span> <span class="nf">get_name</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">str</span><span class="p">:</span>
        <span class="k">return</span> <span class="s1">&#39;Binary tournament selection (experimental)&#39;</span></div></div>
</pre></div>

          </div>
            
        </div>
        <div class="clearfix"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">jMetalPy 1.5.1 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li> 
      </ul>
    </div>
<script type="text/javascript">
  $("#mobile-toggle a").click(function () {
    $("#left-column").toggle();
  });
</script>
<script type="text/javascript" src="../../../_static/js/bootstrap.js"></script>
  <div class="footer">
    &copy; Copyright 2019, Antonio Benítez-Hidalgo. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>.
  </div>
  </body>
</html>